function updatePoints(host) {
	let hw = host.props.width / 2,
		hh = host.props.height / 2
	host.center[0] = host.props.x + hw //- this.parent.props.width / 2
	host.center[1] = host.props.y + hh //- this.parent.props.height / 2
	host.points[0] = [0, -hh]
	host.points[1] = [hw, -hh]
	host.points[2] = [hw, 0]
	host.points[3] = [hw, hh]
	host.points[4] = [0, hh]
	host.points[5] = [-hw, hh]
	host.points[6] = [-hw, 0]
	host.points[7] = [-hw, -hh]
}
class StyleGenerator {
    constructor(host, exclude){
		const _this = this
        const style = {
            width: 'auto',
			height: 'auto',
			x: 0,
			y: 0,
			rotate: 0,
			scale: 1,
			opacity: 1,
			radius: 0,
			border: '',
			shadow: '',
			color: '',
			background: '',
			overflow: 'visible',
			font: '16px/20px arial,sans-serif',
			align: 'left',
			wrap: 'auto',
			textShadow: '',
			textBorder: ''
		}
		const props = Object({}, style, {
			width: 100,
			height: 100,
            scale: [1, 1],
            border: [],
            radius: [0, 0, 0, 0],
            shadow: [],
            textBorder: [],
            textShadow: []
        })
        Object.defineProperties(host, {
            style: {
                value: style
            },
            props: {
                value: props
            }
		})
		const pointAttrs = ['width', 'height', 'x', 'y']
		let timer = null
		for (let name in style) {
            if(exclude && exclude.includes(name)) {
				return Reflect.defineProperty(props, name)
			}
			let oldVal = style[name]
			Object.defineProperty(style, name, {
				set: val => {
                    if (oldVal == val) return
                    let event = {
                        type: 'watch',
                        value: val,
                        oldValue: oldVal,
                        propValue: props[name],
                        oldPropValue: props[name]
                    }
					oldVal = val
					host.emit(event)
					clearTimeout(timer)
					timer = setTimeout(() => {
						props[name] = event.propValue = _this[name] && _this[name](val, props[name])
						updatePoints(host)
						console.log('===')
					},1)
				},
				get() {
					return oldVal
				}
			})
        }
	}
    width(val, old) {
		val = parseFloat(val)
		if (val >= 0) return val
		return old
    }
	height(val, old) {
		val = parseFloat(val)
		if (val >= 0) return val
		return old
	}
	x(val, old) {
		val = parseFloat(val)
		if (!isNaN(val)) return val
		return old
	}
	y(val, old) {
		val = parseFloat(val)
		if (!isNaN(val)) return val
		return old
	}
	rotate(val, old) {
		val = parseFloat(val)
		if (!isNaN(val)) return val * Math.PI / 180
		return old
	}
	scale(val, old) {
		if (!val) return
		let arr = []
		if (Array.isArray(val)) {
			for (let i = 0; i < 2; i++) {
				let n = parseFloat(val[i])
				arr[i] = isNaN(n) ? 1 : n
            }
            return arr
		} else if (typeof (val) === 'number') {
            arr[0] = arr[1] = val
            return arr
		}
		return old
	}
	opacity(val, old) {
		val = parseFloat(val)
		if (val >= 0 && val <= 1) return val
		return old
	}
	border(val, old) {
        let arr = []
        val = (val+'').split(/\s+/)
        for (let i = 0; i < 2; i++) {
            let w = parseFloat(val[i])
            if(!isNaN(w) && w >= 0){
                arr[0] = w
            }
            if(val[i] && /^(#|[a-z])/.test(val[i])){
                arr[1] = val[i]
            }
        }
        if(arr.length === 2) return arr
		return old
	}
	textBorder(val, old) {
		return this.border(val, old)
	}
	color(val, old) {
		if (val) return val
		return old
	}
	background(val, old) {
		if (val) return val
		return old
	}
	radius(val, old) {
		let arr = [],
			i = 0
		if (typeof (val) === 'number' && val >= 0) {
			for (; i < 4; i++) {
				if (val !== arr[i]) {
					arr[i] = val
				}
			}
		} else {
			val = Array.isArray(val) ? val : (typeof (val) === 'string' ? val.split(/\s+/) : null)
			if (val) {
				for (n = 0; i < 4; i++) {
					n = parseFloat(val[i]) || 0
					if(n >= 0) arr[i] = n
				}
			}
        }
        if(arr.length === 4) return arr
		return old
	}
	overflow(val, old) {
        if(val === 'visible' || val === 'hidden') return val
		return old
	}
	font(val, old) {
		if (val) return val
		return old
	}
	align(val, old) {
		if (val === 'left' || val === 'center' || val === 'right') return val
		return old
	}
	wrap(val, old) {
		if (val === 'auto' || val === 'none') return val
		return old
	}
	shadow(val, old) {
		let arr = Array.isArray(val) ? val : (typeof val === 'string' ? val.split(/\s+/) : null)
		let shadow = []
		if (arr && arr.length === 4) {
			for (let i = 0, idx = 1; i < 4; i++) {
				let n = parseFloat(arr[i])
				if (isNaN(n)) {
					if (shadow[0] !== arr[i]) {
						shadow[0] = arr[i]
					}
				} else {
					if (n >= 0) shadow[idx] = n
					idx++
				}
			}
        }
        if(shadow.length === 4) return shadow
		return old
	}
	textShadow(val, old) {
		return this.shadow(val, old)
	}
}

export default StyleGenerator